/* user的增删改查 */
const router = require('koa-router')();
const tools = require('../../module/tool.js');
const DB = require('../../module/db.js');


router.get('/',async (ctx)=>{
    await ctx.render('admin/login');
})

router.post('/doLogin',async (ctx)=>{
    console.log(ctx.request.body)
    let username = ctx.request.body.username;
    let password = ctx.request.body.password;
    //验证用户名密码是否合法
    //去数据库匹配
    //成功以后把用户信息写入session
    let result = await DB.find('user',{"username": username,"password": tools.md5(password)});

    if(result.length > 0){
        console.log(result)
        ctx.session.userinfo = result[0];
        ctx.redirect('/admin')
    }else{
        console.log('fail')
    }

})
module.exports = router.routes();